/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.dataanalysis;

import net.bioteam.appweb.gwt.apps.client.StaticHolder;
import net.bioteam.appweb.gwt.apps.client.Util.ApplicationCallBack;
import net.bioteam.appweb.gwt.apps.clientandserver.dataanalysis.DataAnalysisAction;
import net.bioteam.appweb.gwt.apps.clientandserver.dataanalysis.DataAnalysisActionAsync;
import net.bioteam.appweb.gwt.apps.clientandserver.dataanalysis.FormData;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.events.SelectionChangedHandler;
import com.smartgwt.client.widgets.grid.events.SelectionEvent;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class ServicePanel extends VLayout
{
	public ServicePanel()
	{
		this.setWidth100();
		this.setHeight100();

		// menu
		HLayout bottom = new HLayout();
		bottom.setAlign(Alignment.RIGHT);
		bottom.setWidth(400);
		bottom.setHeight(30);

		Label label = new Label("(Step 2 of 3)");
		label.setHeight(20);
		label.setWidth(100);
		bottom.addMember(label);

		IButton button = new IButton("next->");
		button.addClickHandler(new ClickHandler()
		{

			public void onClick(ClickEvent event)
			{
				if (StaticHolder.servicesGrid.getSelectedRecord() == null)
				{
					SC.say("no analysis is selected. Please select one");
					return;
				}

				final String serviceID = StaticHolder.servicesGrid.getSelectedRecord().getAttribute("filekey");
				ApplicationCallBack<FormData> callback = new ApplicationCallBack<FormData>()
				{

					public void onFailureAfter(Throwable caught)
					{
						StaticHolder.loadingWindow.hide();
						SC.warn(caught.getMessage(), StaticHolder.dumpB);
					}

					public void onSuccessAfter(FormData data)
					{

						Window.setTitle("Data-based analysis: fill parameters");
						DataProgramForm form = new DataProgramForm();
						form.setFormData(data);
						
						StaticHolder.dataProgramForm = form;
						DataProgramFormPanel dp = StaticHolder.getDataProgramFormPanel();
						dp.setProgramForm(form);

						StaticHolder.content.switchPanel(dp);
						StaticHolder.loadingWindow.hide();
					}

					public void execute()
					{
						StaticHolder.setStatusMessage("reinitializing application form ..", 6);
						StaticHolder.loadingWindow.show("loading service...", true);
						DataAnalysisActionAsync proxy = GWT.create(DataAnalysisAction.class);
						proxy.getFormData(serviceID, this);
					}

				};
				callback.execute();

			}
		});
		bottom.addMember(button);

		addMember(bottom);

		ServicesGrid grid = new ServicesGrid();
		StaticHolder.servicesGrid = grid;

		addMember(grid);

		final Label messageLabel = new Label();
		messageLabel.setAlign(Alignment.CENTER);
		messageLabel.setContents("please select an analysis");
		messageLabel.setWidth(500);
		messageLabel.setBackgroundColor("yellow");

		addMember(messageLabel);

		grid.addSelectionChangedHandler(new SelectionChangedHandler()
		{


			public void onSelectionChanged(SelectionEvent event)
			{
				Record record = event.getRecord();
				messageLabel.setContents("Selected analysis: " + record.getAttribute("filekey"));
			}

			
		});

	}

}
